package com.tzy.evaluation.controller;
import com.tzy.common.constant.CacheConstant;
import com.tzy.common.constant.Result;
import com.tzy.common.constant.ResultCode;
import com.tzy.common.controller.BaseController;
import com.tzy.evaluation.service.RedisService;
import com.tzy.evaluation.service.UserLikesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户点赞表 前端控制器
 * </p>
 *
 * @author 谭同学
 * @since 2022-03-26
 */
@Slf4j
@RestController
@RequestMapping("/evaluation/user/liking")
public class UserLikesController extends BaseController {
    @Resource
    private UserLikesService userLikesService;
    @Resource
    private RedisService redisService;
    /**
     * 给用户的评价点赞
     * @param evaluationId
     * @param request
     * @param likedUserId 被点赞的用户编号
     * @return
     */
    @PutMapping("/do")
//    public Result userLikeEvaluation(HttpServletRequest request, @RequestBody UserLikes userLikes){
    public Result likingEvaluation( HttpServletRequest request, int evaluationId, int likedUserId){
        int likingUserId = getUserId(request);
//        log.info("userLikes="+userLikes);
        log.info("evaluationId={},likedUserId={},likingUserId={}",evaluationId,likedUserId,likingUserId);
//        int evaluationId = userLikes.getEvaluationId();
//        int likedUserId = userLikes.getLikingUserId();
//        boolean likeTheEvaluation = userLikesService.likeTheEvaluation(evaluationId, likedUserId, likingUserId);
        boolean likeTheEvaluation = userLikesService.likeTheEvaluation(evaluationId,likedUserId, likingUserId);
        if(likeTheEvaluation){
            return Result.success(likeTheEvaluation);
        }
        return Result.failure(ResultCode.FAILURE);
    }
    @GetMapping("/get/count")
    public Result getCount(@RequestParam("evaluationId") int evaluationId){
        log.info("evaId="+evaluationId);
        Integer totalCount = redisService.getInteger(CacheConstant.EVALUATION_TOTAL_LIKED_COUNT+evaluationId);
        log.info("count"+totalCount);
        return Result.success(totalCount);
    }
    @PutMapping("/set/bit")
    public Boolean setBit(String key,long offset,boolean bit){
        return redisService.setBit(key, offset, bit);
    }
}
